﻿using BCCommon.Enums;
using BCData.WeChatComponent.WeChatMessageRecord;
using BCEntity.WeChatComponent.WeChatMessageRecord;
using System;
using System.Collections.Generic;
using System.Text;
using WeChatSDK.WeChatDto.Conversation.ReceivingMessage;
using WeChatSDK.WeChatDto.Conversation.ReceivingMessage.EventMessage;

namespace BCService.WeChatComponent.WeChatHandleService.EventMessageService.SubscribeEventMessageService
{
    /// <summary>
    /// 关注/取消关注事件处理
    /// </summary>
    public class SubscribeEventMessageService : IWeChatMessageHandle
    {
        private readonly IWeChatMessageRecordData weChatMessageRecordData;

        public SubscribeEventMessageService(IWeChatMessageRecordData weChatMessageRecordData)
        {
            this.weChatMessageRecordData = weChatMessageRecordData;
        }

        public string HandleMessage(ReceivingMessageDto message)
        {
            SubscribeEventMessageDto subscribeEventMessageDto = (SubscribeEventMessageDto)message;
            WeChatMessageRecordEntity weChatMessageRecordEntity = this.RecordReceivingMessage(subscribeEventMessageDto);
            string response = "success";
            return response;
        }

        private WeChatMessageRecordEntity RecordReceivingMessage(SubscribeEventMessageDto subscribeEventMessageDto)
        {
            WeChatMessageRecordEntity entity = new WeChatMessageRecordEntity()
            {
                ToUserName = subscribeEventMessageDto.ToUserName,
                FromUserName = subscribeEventMessageDto.FromUserName,
                MessageType = WeChatMessageType.Event,
                MessageId = subscribeEventMessageDto.MsgId,
                EventName = subscribeEventMessageDto.Event,
                Title = subscribeEventMessageDto.Event== "subscribe" ? "用户已关注此公众号！":"用户已取消关注此公众号！",
                IsReply = false
            };
            return this.weChatMessageRecordData.Add(entity);
        }
    }
}
